Model estimates

load("C:\\Users\\rolek.brian\\OneDrive - The Peregrine Fund\\Documents\\Projects\\Ridgways IPM\\outputs\\ipm_simp.Rdata")
load("data/data.rdata")
library ('MCMCvis')
library ('coda')

out <- list(as.mcmc(post[[1]]), 
            as.mcmc(post[[2]]), 
            as.mcmc(post[[3]]),
            as.mcmc(post[[4]]),
            as.mcmc(post[[5]]),
            as.mcmc(post[[6]]),
            as.mcmc(post[[7]]),
            as.mcmc(post[[8]]),
            as.mcmc(post[[9]]),
            as.mcmc(post[[10]]) )


outp <- MCMCpstr(out, type="chains")

Plot model estimates of demographic rates. Life Stages are abbreviated as B = breeder, NB = nonbreeder, FY = first year. First-year abundance accounts for translocated birds. # Print parameter estimates Parameter estimates for input into a population viability analysis.

plt  <- function(object, params,...) {
  MCMCplot(object=out, 
           params=params, 
           guide_axis=TRUE, 
           HPD=TRUE, ci=c(80, 95), horiz=FALSE, 
           #ylim=c(-10,10),
           ...)
}

par(mfrow=c(1,2))
plt(object=out, 
    params=paste0("mus[",1:8, ", 1]"), 
    exact=TRUE, ISB=FALSE, 
    ylim=c(0,1),
    main="Overall means\n Los Haitises", 
    labels=c("FY survival", "NB survival", "B survival",
             "FY to B", "NB to B", "B to NB",
             "NB detection", "B detection")
)

plt(object=out, 
    params=paste0("mus[",1:8, ", 2]"), 
    exact=TRUE, ISB=FALSE, 
    ylim=c(0,1),
    main="Overall means\n Punta Cana", 
    labels=c("FY survival", "NB survival", "B survival",
             "FY to B", "NB to B", "B to NB",
             "NB detection", "B detection"))

par(mfrow=c(1,1))
plt(object=out, 
    params="betas", 
    main= "Translocation effects",
    labels=c("FY survival", "NB survival", "B survival",
             "FY to B", "NB to B", "B to NB",
             "NB detection", "B detection"))

par(mfrow=c(1,1))
sds <- paste0("sds[", 1:9, "]")
plt(object=out, params=sds,
    exact=TRUE, ISB=FALSE,
    main="Temporal SDs (synchrony among sites)", 
    labels=c("FY survival", "NB survival", "B survival",
             "FY to B", "NB to B", "B to NB",
             "NB detection", "B detection",
             "Fecundity"))

sds2 <- paste0("sds2[", 1:9, "]")
plt(object=out, params=sds2,
    exact=TRUE, ISB=FALSE,
    main="Site-temporal SDs", 
    labels=c("FY survival", "NB survival", "B survival",
             "FY to B", "NB to B", "B to NB",
             "NB detection", "B detection",
             "Fecundity"))

# Correlations among vital rates
# Plot is messy with only a few strong correlations
ind <- 1
Rs <- R2s <- c()
for (i in 1:(nrow(outp$R)-1)){
  for (j in (i+1):nrow(outp$R)){
    Rs[ind] <- paste0("R[",i,", ", j, "]")
    R2s[ind] <- paste0("R2[",i,", ", j, "]")
    ind <- ind+1
  }}
# Both indices correspond to the parameters
# in the order listed above
# R[parameter1, parameter2], where R is the correlation between them
par(mfrow=c(1,1))
plt(object=out, params=Rs, exact=TRUE, ISB=FALSE, 
    main="Correlations btw demographic rates\n over time (synchrony)",
    xlab = "Rhos", guide_lines=TRUE)

# There's little temporal synchrony between sites
# R[1,3] appears slightly correlated and that suggests
# First year and breeder survival have some 
# synchrony. 

plt(object=out, params=R2s, exact=TRUE, ISB=FALSE, 
    main="Correlations btw demographic rates\n over time and sites",
    xlab = "Rhos", guide_lines=TRUE)

# Within each site 
# First year and breeder survival appear correlated (again R[1,3])
# as well as detection probability of breeders and fecundity (R[8,9])

# Annual averages for integration into the population model
labs <- c(paste0("LH ",2011:2023), paste0("PC ",2011:2023))
plt(object=out, params="mn.phiFY", ylim=c(0,1),
    main="First-year survival", labels = labs,
    xlab = "Year", ylab= "Survival")
abline(v=13.5, lwd=2)

plt(object=out, params="mn.phiA", ylim=c(0,1),
    main="Adult nonbreeder", labels = labs,
    xlab = "Year", ylab= "Survival")
abline(v=13.5, lwd=2)

plt(object=out, params="mn.phiB", ylim=c(0,1),
    main="Breeder", labels = labs,
    xlab = "Year", ylab= "Survival")
abline(v=13.5, lwd=2)

plt(object=out, params="mn.psiFYB", ylim=c(0,1),
    main="First-year to breeder", labels = labs,
    xlab = "Year", ylab= "Recruitment")
abline(v=13.5, lwd=2)

plt(object=out, params="mn.psiAB", ylim=c(0,1),
    main="Adult nonbreeder to breeder", labels = labs,
    xlab = "Year", ylab= "Recruitment")
abline(v=13.5, lwd=2)

plt(object=out, params="mn.psiBA", ylim=c(0,1),
    main="Adult breeder to nonbreeder", labels = labs,
    xlab = "Year", ylab= "Recruitment")
abline(v=13.5, lwd=2)

plt(object=out, params="mn.pA", ylim=c(0,1),
    main="Nonbreeder", labels = labs,
    xlab = "Year", ylab= "Detection")
abline(v=13.5, lwd=2)

plt(object=out, params="mn.pB", ylim=c(0,1),
    main="Breeder", labels = labs,
    xlab = "Year", ylab= "Detection")
abline(v=13.5, lwd=2)

Fecundity

# Averages
par(mfrow=c(1,1))
plt(object=out, 
    params=c("lmu.f"), 
    labels= c("Fecundity\n(log scale)\nLos Haitises",
              "Fecundity\n(log scale)\nPunta Cana"))

# gamma = nest treatment effect on fecundity
plt(object=out, 
    params=c("gamma"), 
    main="Anti-Parasitic Fly\nTreatment Effects", ylim=c(0,3))

plt(object=out, params="mn.f",
    main="", labels=labs,
    xlab = "Year", ylab= "Fecundity")
abline(v=13.5, lwd=2)

# So what's up with fecundity at LHNP? Let's examine the raw data.

# Are fewer nests being detected? No
# This would be consistent with lower detection prob of breeders.
nnests <- tapply(datl$f, list(constl$year.nest, constl$site.nest), length)
plot(2011:2023, nnests[,1], type="b",
     ylab="Number of nests detected", xlab="",
     main="Los Haitises")

# Are fewer fledglings being detected? Yes
ndet <- tapply(datl$f, list(constl$year.nest, constl$site.nest), sum)
plot(2011:2023, ndet[,1], type="b",
     ylab="Number of fledglings detected", xlab="",
     main="Los Haitises")

# Has the proportion of nests treated changed? Yes it has decreased.
ptreated <- tapply(constl$treat.nest, list(constl$year.nest, constl$site.nest), mean)
plot(2011:2023, ptreated[,1], type="b",
     ylab="Proportion of nests treated", xlab="",
     main="Los Haitises")

# Have the number of nests treated decreased? Maybe.
# The number of nests dropped slightly.
ntreated <- tapply(constl$treat.nest, list(constl$year.nest, constl$site.nest), sum)
plot(2011:2023, ntreated[,1], type="b",
     ylab="Number of nests treated", xlab="",
     main="Los Haitises")

# How do these line up with the number of breeders counted (Leah's dataset) at LH
# So the number of nests increased but the number of breeders decreased.
# That's confusing and doesn't make sense. Maybe that google sheet
# would be better to use given these discrepancies as you suggested. 
# No guarantees it will fix the average fecundity problem though.
# Perhaps it's density dependence kicking in? The number of breeders could
# be high enough that it's depressing fecundity.
plot(2011:2023, datl$counts[3,,1], type="b",
     ylab="Number of breeders detected", xlab="",
     main="Los Haitises")

# In summary the number of nest treatments remains relatively stable, 
# but it appears that the proportion of nests treated has
# declined because of an increase in the number of nests detected
# perhaps depressing the average affect on fecundity.
# Could it be that there are so many nests that the treatment effects
# are small at this large population level?
# Or density dependence may be depressing fecundity. 
# Count data of breeders do not align.

Model diagnostics

MCMCtrace(post, pdf=FALSE, params= "sds")

MCMCtrace(post, pdf=FALSE, params= "sds2")

MCMCtrace(post, pdf=FALSE, params= "mus")

MCMCtrace(post, pdf=FALSE, params= "betas")

MCMCtrace(post, pdf=FALSE, params= Rs, exact=TRUE, ISB=FALSE)

MCMCtrace(post, pdf=FALSE, params= R2s, exact=TRUE, ISB=FALSE)

# Goodness of fit check
fit.check <- function(out, ratio=FALSE, 
                      name.rep="f.dmape.rep", 
                      name.obs="f.dmape.obs",
                      jit=100,
                      ind=1,
                      lab=""){
  par(mfrow=c(1,1))
  # plot mean absolute percentage error
  samps <- MCMCpstr(out, "all", type="chains")
  rep <- samps[name.rep][[1]][ind,]
  obs <- samps[name.obs][[1]][ind,]
  mx <- max(c(rep, obs))
  mn <- min(c(rep, obs))
  plot(jitter(obs, amount=jit), 
       jitter(rep, amount=jit),
       main=paste0("Mean absolute percentage error\n",lab),
       ylab="Discrepancy replicate values",
       xlab="Discrepancy observed values", 
       xlim=c(mn,mx), ylim=c(mn,mx), 
       pch=16, cex=0.5, col="gray10")
  curve(1*x, from=mn, to=mx, add=T, lty=2, lwd=2, col="blue")
  bp1 <- round(mean(rep > obs),2)
  loc <- ifelse(bp1 < 0.5, "topleft", "bottomright")
  legend(loc, legend=bquote(p[B]==.(bp1)), bty="n", cex=2)
  
  if (ratio==TRUE){
    t.rep <- samps["tvm.rep"][[1]][ind,]
    t.obs <- samps["tvm.obs"][[1]][ind,]
    # plot variance/mean ratio
    hist(t.rep, nclass=50,
         xlab="variance/mean ", main=NA, axes=FALSE)
    abline(v=t.obs, col="red")
    axis(1); axis(2)
  }
  return(list('Bayesian p-value'=bp1))
}

# check goodness-of-fit for fecundity
fit.check(out, ratio=F,
          name.rep="f.dmape.rep", 
          name.obs="f.dmape.obs",
          ind=1,
          lab="Fecundity-Neg binomial", jit=300)

## $`Bayesian p-value`
## [1] 0.94